package com.uam.core.entity.uitls;


import com.uam.core.entity.model.GeoPoint;

/**
 * WGS-84：是国际标准，GPS坐标（Google Earth使用、或者GPS模块）
 * GCJ-02：中国坐标偏移标准，Google Map、高德、腾讯使用
 * BD-09：百度坐标偏移标准，Baidu Map使用
 * Mercator：墨卡托投影
 *
 * @author: heguanglong
 * @date: 2020-12-29
 */
public class GpsFormat {

    public static final double PI = 3.14159265358979324D;
    public static final double X_PI = 3.14159265358979324D * 3000.0 / 180.0;

    private static final double CHINA_LNG_MAX = 137.8347;
    private static final double CHINA_LNG_MIN = 72.004;

    private static final double CHINA_LAT_MAX = 55.8271;
    private static final double CHINA_LAT_MIN = 0.8293;

    /**
     * WGS-84 to GCJ-02
     * WGS84坐标系：即地球坐标系，国际上通用的坐标系。
     * 设备一般包含GPS芯片或者北斗芯片获取的经纬度为WGS84地理坐标系,
     */
    public static GeoPoint wgs84ToGcj02(double wgsLng, double wgsLat) {
        if (outOfChina(wgsLng, wgsLat)) {
            return new GeoPoint(wgsLng, wgsLat);
        }
        GeoPoint d = delta(wgsLng, wgsLat);
        return new GeoPoint(d.getX() + wgsLng, d.getY() + wgsLat);
    }


    /**
     * GCJ-02 to WGS-84
     */
    public static GeoPoint gcjDecrypt(double gcjLng, double gcjLat) {
        if (outOfChina(gcjLng, gcjLat)) {
            return new GeoPoint(gcjLng, gcjLat);
        }
        GeoPoint d = delta(gcjLng, gcjLat);
        return new GeoPoint(gcjLng - d.getX(), gcjLat - d.getY());
    }

    /**
     * GCJ02坐标转换BD09坐标
     * GCJ02坐标系：即火星坐标系，
     * 是由中国国家测绘局制订的地理信息系统的坐标系统。由WGS84坐标系经加密后的坐标系。
     */
    public static GeoPoint gcj02Dd09(double gcjLng, double gcjLat) {
        double x = gcjLat;
        double y = gcjLng;
        double z = Math.sqrt(x * x + y * y) + 0.00002D * Math.sin(y * X_PI);
        double theta = Math.atan2(y, x) + 0.000003D * Math.cos(x * X_PI);
        double bdLat = z * Math.sin(theta) + 0.006D;
        double bdLon = z * Math.cos(theta) + 0.0065D;
        return new GeoPoint(bdLon, bdLat);
    }

    /**
     * BD09坐标转换GCJ02坐标
     */
    public static GeoPoint bd09ToGcj02(double bdLng, double bdLat) {
        double x = bdLng - 0.0065d, y = bdLat - 0.006d;
        double z = Math.sqrt(x * x + y * y) - 0.00002d * Math.sin(y * X_PI);
        double theta = Math.atan2(y, x) - 0.000003d * Math.cos(x * X_PI);
        double gcjLon = z * Math.cos(theta);
        double gcjLat = z * Math.sin(theta);
        return new GeoPoint(gcjLon, gcjLat);
    }

    /**
     * 墨卡托投影
     * 墨卡托(Mercator)投影，又名“等角正轴圆柱投影”，
     * 荷兰地图学家墨卡托（Mercator）在1569年拟定，
     * 假设地球被围在一个中空的圆柱 里，其赤道与圆柱相接触，
     * 然后再假想地球中心有一盏灯，把球面上的图形投影到圆柱体上，
     * 再把圆柱体展开，这就是一幅标准纬线为零度（即赤道）的“墨卡托投 影”绘制出的世界地图。
     */
    public static GeoPoint wgs84Mercator(double wgsLng, double wgsLat) {
        double x = wgsLng * 20037508.34D / 180.0;
        double y = Math.log(Math.tan((90.0 + wgsLat) * PI / 360.0)) / (PI / 180.);
        y = y * 20037508.34D / 180.0;
        return new GeoPoint(x, y);

        /*
        if ((Math.abs(wgsLng) > 180 || Math.abs(wgsLat) > 90))
             return null;
        double x = 6378137.0 * wgsLng * 0.017453292519943295D;
        double a = wgsLat * 0.017453292519943295D;
        double y = 3189068.5 * Math.log((1.0 + Math.sin(a)) / (1.0 - Math.sin(a)));
        return new Point(x, y);
        */
    }

    /**
     * Mercator_WGS84
     */
    public static GeoPoint mercatorWgs84(double mercatorLng, double mercatorLat) {
        double x = mercatorLng / 20037508.34d * 180.;
        double y = mercatorLat / 20037508.34d * 180.;
        y = 180 / PI * (2 * Math.atan(Math.exp(y * PI / 180.0)) - PI / 2);
        return new GeoPoint(x, y);

        /*
        if (Math.abs(mercatorLng) < 180 && Math.abs(mercatorLat) < 90)
             return null;
        if ((Math.abs(mercatorLng) > 20037508.3427892D) || (Math.abs(mercatorLat) > 20037508.3427892d))
            return null;
        double a = mercatorLng / 6378137.0D * 57.295779513082323D;
        double x = a - (Math.floor(((a + 180.0) / 360.0)) * 360.0);
        double y = (1.5707963267948966D - (2.0 * Math.atan(Math.exp((-1.0 * mercatorLat) / 6378137.0d)))) * 57.295779513082323d;
        return new Point(x, y);
        */
    }

    /**
     * 求两坐标距离
     */
    public static double distance(double lngA, double latA, double lngB, double latB) {
        int earthR = 6371000;
        double x = Math.cos(latA * PI / 180.) * Math.cos(latB * PI / 180.) * Math.cos((lngA - lngB) * PI / 180);
        double y = Math.sin(latA * PI / 180.) * Math.sin(latB * PI / 180.);
        double s = x + y;
        if (s > 1) {
            s = 1;
        }
        if (s < -1) {
            s = -1;
        }
        double alpha = Math.acos(s);
        double distance = alpha * earthR;
        return distance;
    }


    private static boolean outOfChina(double lng, double lat) {
        if (lng < CHINA_LNG_MIN || lng > CHINA_LNG_MAX) {
            return true;
        }
        if (lat <CHINA_LAT_MIN || lat > CHINA_LAT_MAX) {
            return true;
        }
        return false;
    }

    private static double transformLat(double x, double y) {
        double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));
        ret += (20.0 * Math.sin(6.0 * x * PI) + 20.0 * Math.sin(2.0 * x * PI)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(y * PI) + 40.0 * Math.sin(y / 3.0 * PI)) * 2.0 / 3.0;
        ret += (160.0 * Math.sin(y / 12.0 * PI) + 320 * Math.sin(y * PI / 30.0)) * 2.0 / 3.0;
        return ret;
    }

    private static double transformLon(double x, double y) {
        double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));
        ret += (20.0 * Math.sin(6.0 * x * PI) + 20.0 * Math.sin(2.0 * x * PI)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(x * PI) + 40.0 * Math.sin(x / 3.0 * PI)) * 2.0 / 3.0;
        ret += (150.0 * Math.sin(x / 12.0 * PI) + 300.0 * Math.sin(x / 30.0 * PI)) * 2.0 / 3.0;
        return ret;
    }

    private static GeoPoint delta(double lng, double lat) {
        // a = 6378245.0, 1/f = 298.3
        // b = a * (1 - f)
        // ee = (a^2 - b^2) / a^2;
        //  a: 卫星椭球坐标投影到平面地图坐标系的投影因子。
        double a = 6378245.0;
        //  ee: 椭球的偏心率。
        double ee = 0.00669342162296594323;
        double dLat = transformLat(lng - 105.0, lat - 35.0);
        double dLon = transformLon(lng - 105.0, lat - 35.0);
        double radLat = lat / 180.0 * PI;
        double magic = Math.sin(radLat);
        magic = 1 - ee * magic * magic;
        double sqrtMagic = Math.sqrt(magic);
        dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * PI);
        dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * PI);
        return new GeoPoint(dLon, dLat);
    }
}
